package com.edu.robot.core.service.impl;

import com.edu.buservice.facade.facade.TenantFacade;
import com.edu.chat.facade.model.dto.SceneInfoDTO;
import com.edu.chat.facade.model.dto.SessionInfoDetailDTO;
import com.edu.robot.core.model.QueryContext;
import com.edu.robot.core.model.SessionContext;
import com.edu.robot.core.service.ModelInvoke;
import com.edu.robot.core.service.ModelManger;
import com.edu.robot.core.service.ModelStrategy;
import com.edu.robot.model.enums.ResultCodeEnum;
import com.edu.robot.model.error.BizException;
import com.edu.robot.modules.robot.dto.RobotConfigDTO;
import com.edu.robot.modules.robot.dto.RobotInfoDTO;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-23 17:04
 **/


public abstract class AbstractModelInvoke implements ModelInvoke, ModelStrategy {
	@Autowired
	public ModelManger modelManger;

	@DubboReference(interfaceClass = TenantFacade.class,timeout = 3000)
	private TenantFacade tenantFacade;

	@Override
	public void process(QueryContext queryContext) {

		//前置校验
		preCheck(queryContext);

		//构建sessionContext
		SessionContext sessionContext = buildSessionContext(queryContext);

		//构造模型参数
		buildModel(sessionContext);

		//执行构造提示词模板
	    buildPrompt(sessionContext);

		//执行模型请求
		invokeModel(sessionContext);

		//TODO token计费数据落库
		int token = sessionContext.getToken();

		tenantFacade.updateToken(queryContext.getTenantId(), (long) token);

	}

	private void preCheck(QueryContext queryContext){

		if(queryContext == null){
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"请求参数为空！");
		}

		if(StringUtils.isBlank(queryContext.getUserInput())){
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"用户输入为空！");
		}
		RobotInfoDTO robotInfoDTO = queryContext.getRobotInfoDTO();
		if(Objects.isNull(robotInfoDTO)){
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"机器人信息为空！");
		}
		RobotConfigDTO robotConfig = robotInfoDTO.getRobotConfig();
		if(Objects.isNull(robotConfig)){
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"机器人配置信息为空！");
		}

		String modelConfigId = robotConfig.getModelConfigId();

		if(StringUtils.isBlank(modelConfigId)){
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"模型配置ID为空！");
		}

		if(StringUtils.isBlank(queryContext.getTenantId())){
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"租户ID为空！");
		}

		if (StringUtils.isBlank(queryContext.getSessionId())) {
			throw new BizException(ResultCodeEnum.INTERNAL_ERROR,"会话ID为空！");
		}

	}

	//构建sessionContext
	private SessionContext buildSessionContext(QueryContext queryContext) {
		SessionContext sessionContext = new SessionContext();
		sessionContext.setCallback(queryContext.getApiCallback());
		sessionContext.setSessionId(queryContext.getSessionId());
		sessionContext.setQuestion(queryContext.getUserInput());
		sessionContext.setTenantId(queryContext.getTenantId());
		sessionContext.setIntentEnum(queryContext.getIntentEnum());
		sessionContext.setRobot(queryContext.getRobotInfoDTO());

		// 使用 Optional 处理 RobotConfig 的公共知识库 ID
		Optional.ofNullable(queryContext.getRobotInfoDTO())
				.map(RobotInfoDTO::getRobotConfig)
				.map(RobotConfigDTO::getPublicKnowledgeId)
				.ifPresent(sessionContext::setPublicKnowledgeId);

		// 使用 Optional 处理 SessionInfoDetail 的私有知识库 ID
		Optional.ofNullable(queryContext.getSessionInfoDetailDTO())
				.map(SessionInfoDetailDTO::getSceneInfo)
				.map(SceneInfoDTO::getLibraryIds)
				.filter(StringUtils::isNotBlank)
				.map(libs -> List.of(libs.split(",")))
				.ifPresent(sessionContext::setPrivateKnowledgeId);

		Optional.ofNullable(queryContext.getSessionInfoDetailDTO())
				.map(SessionInfoDetailDTO::getSceneInfo)
				.map(SceneInfoDTO::getSceneId)
				.ifPresent(sessionContext::setSceneId);

		return sessionContext;
	}


}
